﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using EPiServer;
using EPiServer.Core;
using EPiServer.Filters;
using PageTypeBuilder;
using TemplateFoundation.Configuration;
using TemplateFoundation.PageTypes;

namespace TemplateFoundation.Tags
{
    /// <summary>
    /// Provides functionality for working with page tags
    /// </summary>
    public class TagFactory
    {
        private static readonly TagFactory _instance = new TagFactory();

        TagFactory()
        {
            
        }

        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <value>The instance.</value>
        public static TagFactory Instance
        {
            get
            {
                return _instance;
            }
        }

        /// <summary>
        /// Returns all tags
        /// </summary>
        /// <returns></returns>
        public TagCollection GetTags()
        {
            return GetTags(string.Empty);
        }

        /// <summary>
        /// Returns all tags for the specified language branch
        /// </summary>
        /// <returns></returns>
        public TagCollection GetTags(string languageBranch)
        {
            PageDataCollection tagPages;
            
            if(string.IsNullOrEmpty(languageBranch))
            {
                tagPages=DataFactory.Instance.GetChildren(Settings.Instance.TagsContainer);
            }
            else
            {
                tagPages=DataFactory.Instance.GetChildren(Settings.Instance.TagsContainer, new LanguageSelector(languageBranch));
            }

            var tags = new TagCollection();

            foreach (var tagPage in tagPages)
            {
                tags.Add(new Tag(tagPage.PageName, (ArticleTagListingPageBase)tagPage));
            }

            return tags;
        }
    }
}
